package com.landeb.shop.huo.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.landeb.shop.R;
import com.landeb.shop.huo.act.ShopSearchResultActivity;
import com.landeb.shop.huo.bean.BeanActShopList;
import com.landeb.shop.huo.view.FixedGridView;

import java.util.List;

/**
 * Created by blade on 2016/12/8.
 */

public class ActShopListLvAdapter extends BaseAdapter {

    private List<BeanActShopList.DataBean> mList;
    private Context mContext;
    private LayoutInflater mInflater;

    public ActShopListLvAdapter(Context context, List<BeanActShopList.DataBean> list) {
        mContext = context;
        mList = list;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder = null;
        if (convertView == null) {

            convertView = mInflater.inflate(R.layout.item_act_shop_list_lv, parent, false);

            holder = new ViewHolder();
            holder.mTitle = (TextView) convertView.findViewById(R.id.tv_itemName_act_shop_list_lv);
            holder.mGridView = (FixedGridView) convertView.findViewById(R.id.gv_act_shop_list_lv);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        holder.mTitle.setText(mList.get(position).getCatename());
        holder.mTitle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO: 2016/12/8 点击标题
                String cid = mList.get(position).getId();
                String id = mList.get(position).getStore_id();

                ShopSearchResultActivity.toAct(mContext,id,cid);

//                Toast.makeText(mContext, id, Toast.LENGTH_SHORT).show();

            }
        });

        if (mList.get(position).getChild() != null) {
            ActShopListLvGvAdapter mAdapter = new ActShopListLvGvAdapter(mContext, mList.get(position).getChild());
            holder.mGridView.setAdapter(mAdapter);
            holder.mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int gvPosition, long id) {
                    // TODO: 2016/12/8 选择子项
                    String cId = mList.get(position).getChild().get(gvPosition).getId();

                    String store_id = mList.get(position).getChild().get(gvPosition).getStore_id();

                    ShopSearchResultActivity.toAct(mContext,store_id,cId);
//                    Toast.makeText(mContext, itemId, Toast.LENGTH_SHORT).show();

                }
            });
        }


        return convertView;
    }

    private class ViewHolder {
        private TextView mTitle; //文字
        private FixedGridView mGridView;
    }
}
